setwd("/home1/30/jc227089/SRE/gvar/PBS")

stps<-c(1,2,4,5,10,20)
lda<-c(2, 4, 6, 8, 10)
dct<-c(0, 0.1, 0.5)

script.file<-'/home1/30/jc227089/evo-dispersal/SRE/gvar/Gradient_var.R'

for (ii in 1:length(stps)) {
	for (jj in 1:length(lda)){
		for (kk in 1:length(dct)){
			fid<-(ii-1)*length(lda)*length(dct)+(jj-1)*length(dct)+kk #file.ID
			##create the sh file
			zz = file(paste("SREgvar", fid,'.sh',sep=''),'w')
			cat('##################################\n',file=zz)
			cat('#!/bin/sh\n',file=zz)
			cat('cd $PBS_O_WORKDIR\n',file=zz)
			cat("R CMD BATCH --no-save --no-restore '--args rr=3 file.ID=",fid, " stps=", stps[ii], " lv=", lda[jj], " dc=", dct[kk],"' ", sep="", file=zz)
			cat(script.file, " ", paste("SREgvar", fid,'.Rout',sep=''), "\n", sep="",file=zz)
			cat('##################################\n',file=zz)
			close(zz)
			
			#submit the job
			system(paste("qsub -m n ", paste("SREgvar", fid,".sh",sep=""),sep=""))
		}
	}
}
